<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText" @tap="onreturn()">返回</block>
			<block slot="content">用户编辑</block>
		</cu-custom>

		<view class="margin-top">
			<form id="eform">
				<view class="cu-form-group ">
					<view class="title  from_width">用户编号</view>
					<input type="text" v-model="userdata.no" name="no" placeholder="请输入用户编号"></input>
				</view>
				<view class="cu-form-group ">
					<view class="title  from_width">用户名</view>
					<input type="text" v-model="userdata.name" placeholder="请输入真实姓名" name="name"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">登录密码</view>
					<input type="text" v-model="userdata.pwd" placeholder="请输入登录密码" name="pwd"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">联系电话</view>
					<input type="text" v-model="userdata.phone" placeholder="请输入联系电话" name="phone"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">职务</view>
					<input type="text" v-model="userdata.zhiwu" placeholder="请输入职务" name="zhiwu"></input>
				</view>
				<view class="bg-white align-center">
					<view class="flex flex-wrap">
						<view class="basis-df padding">
							<button class="bg-blue xs text-lg" @tap="tj()">提交</button>
						</view>
						<view class="basis-df  padding">
							<button class="bg-blue xs text-lg" form-type="reset" refBtn>清空</button>
						</view>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import uu from "@/common/util.js";

	export default {
		data() {
			return {
				op_type: '',
				userdata: {
					id: '0',
					no: '',
					name: '',
					phone: '',
					zhiwu: '',
					state: '0',
					pwd: '',
				},
			}
		},
		onLoad(op) {
			console.log('当前操作是' + op.op);
			this.op_type = op.op;
			if (op.op == 'edit') {
				this.userdata.id = op.id;
				console.log('onload接收的参数' + op.id);
				this.getuserById();
			} else { //==add

			}

		},
		methods: {
			// 返回上一页
			onreturn() {
				uni.reLaunch({
					url: '../work/usermanage'
				});
			},
			backButton() {
				uni.navigateBack({
					delta: 1
				});
			},
			tj() {
				var _self = this;
				let user = this.userdata;
				console.log(JSON.stringify(user));

				//验证用户是否存在，验证手机号是否正确
				if (!uu.checkMobile(user.phone)) {
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none'
					});
					return;
				}


				if (_self.op_type == 'edit') {
					//修改 
					let sqls = "update c_user set no='" + user.no + "',pwd='" + user.pwd + "',state=" +
						user.state +
						",zhiwu='" + user.zhiwu + "',phone='" + user.phone + "' where id='" +
						user.id + "'";
					plus.sqlite.executeSql({
						name: 'test',
						sql: sqls,
						success: function(e) {
							uni.showToast({
								title: '修改成功！',
								icon: "success"
							});
							console.log('executeSql success!');
							_self.userdata.reset();
							uni.hideLoading();
						},
						fail: function(e) {
							uni.showToast({
								title: '修改失败！',
								icon: "none"
							});
							console.log('executeSql failed: ' + JSON.stringify(e));
							uni.hideLoading();
						}
					});
				} else {
					//添加
					//验证用户是否存在
					plus.sqlite.selectSql({
						name: 'test',
						sql: "select count(*) c from c_user where name='" + user.name + "'",
						success: function(data) {
							if (parseInt(data[0].c) > 0) {
								console.log('存在');
								uni.showToast({
									title: '该用户已存在',
									icon: 'none'
								});
								return;
					
							} else {
								//不存在则添加
								console.log('不存在' + _self.op_type);
					
								if (_self.op_type == 'add') {
									uni.showLoading({
										title: '提交中...'
									})
									//生成用户唯一ID
									user.id = Number(Math.random().toString().substr(3, 3) + Date.now()).toString(36);
					
									let sqls = "insert into c_user values('" + user.id + "','" + user.name + "','" + user.no + "','" + user.pwd +
										"','" +
										user.state + "','" + user.zhiwu + "','" + user.phone + "');";
					
									plus.sqlite.executeSql({
										name: 'test',
										sql: sqls,
										success: function(e) {
											uni.showToast({
												title: '添加成功！',
												icon: "success"
											});
											console.log('executeSql success!');
											uni.hideLoading();
										},
										fail: function(e) {
											uni.showToast({
												title: '添加失败！',
												icon: "none"
											});
											uni.hideLoading();
											console.log('executeSql failed: ' + JSON.stringify(e));
										}
									});
								} else {
					
								}
								_self.backButton();
					
							} 
						},
						fail: function(e) {
							console.log('selectSql failed: ' + JSON.stringify(e));
						}
					});
				} 
			},
			clearForm() {
				//h5端可以 //翻源码得知的清空表单方法
				console.log('触发清空表单事件');
				this.$refs.refBtn.$dispatch('Form', 'uni-form-reset', {
					type: 'reset'
				})
			},
			//获取当前用户信息
			getuserById() {
				var _self = this;
				plus.sqlite.selectSql({
					name: 'test',
					sql: "select * from c_user where id='" + this.userdata.id + "'",
					success: function(data) {

						_self.userdata = data[0];
						console.log(_self.userdata);
					},
					fail: function(e) {
						console.log('selectSql failed: ' + JSON.stringify(e));
					}
				});

			},

		}
	}
</script>

<style>
	page {
		width: 100vw;
		position: relative;
		background-color: #fff;
	}

	.from_width {
		width: 200upx;
	}
</style>
